package br.com.fogas.webservice.soap.impl;

import android.content.Context;
import br.com.fogas.utils.FogasUtility;
import br.com.fogas.utils.StringUtility;
import br.com.fogas.utils.exception.ParseXmlException;
import br.com.fogas.webservice.exception.RespostaWebServiceException;
import br.com.fogas.webservice.exception.SemConexaoInternetException;
import br.com.fogas.webservice.exception.ServidorForaException;
import br.com.fogas.webservice.exception.TempoEsperaRespostaException;
import br.com.fogas.webservice.exception.WebServiceException;
import br.com.fogas.webservice.parser.BaseParser;
import br.com.fogas.webservice.parser.material.MaterialResponse;
import br.com.fogas.webservice.soap.POCallbackSOAP;
import br.com.fogas.webservice.soap.TimeConnectionServer;

public class MaterialSOAP extends POCallbackSOAP<MaterialResponse> {

	public MaterialSOAP(Context context, BaseParser parser) {
		super(context, parser);
	}

	@Override
	public String getInterface() {
		return "ZCI_GET_MATERIAL&interfaceNamespace=urn:sap-com:document:sap:rfc:functions";
	}
	
	@Override
	public TimeConnectionServer timeOutConnectionServer() {
		return new TimeConnectionServer(false, 0, 0);
	}

	@Override
	public String getXmlResponse() {
		String regex = "<ns0:ZCI_GET_MATERIAL.Response[^>]*>(.*?)</ns0:ZCI_GET_MATERIAL.Response>";
		String responseXml = FogasUtility.string.getSubstringByRegex(regex, baseParser.getResponseXML());
		return StringUtility.normalizeSpace(responseXml);
	}

	@Override
	public MaterialResponse getResponse()
			throws SemConexaoInternetException, ParseXmlException,
			TempoEsperaRespostaException, ServidorForaException,
			RespostaWebServiceException, WebServiceException {
		super.sendRequest();
		return (MaterialResponse) FogasUtility.xml.unParseXML(MaterialResponse.class, getXmlResponse());
	}
}
